package com.example.busquerysystem.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.example.busquerysystem.R;
import com.example.busquerysystem.bean.BusStation;
import com.example.busquerysystem.inter.Station;
import com.example.busquerysystem.utils.HttpUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import javax.xml.transform.Result;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 公交站点查询
 */
public class SiteFragment extends Fragment implements View.OnClickListener {

    private EditText etcity;
    private EditText etStation;
    private Button chaxun;
    private RecyclerView recycler_view;
    private MyRecycleView myAdapter;
    List<BusStation.ResultBean> data = new ArrayList<BusStation.ResultBean>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View site = inflater.inflate(R.layout.activity_site, container, false);
        return site;
    }


    public void initView() {
        etStation = getView().findViewById(R.id.et_station);
        chaxun = getView().findViewById(R.id.btn_station);
        etcity = getView().findViewById(R.id.et_city);
        recycler_view = getView().findViewById(R.id.my_recyclerVIew);
        chaxun.setOnClickListener(this);



    }

    @Override
    public void onStart() {
        super.onStart();
//        SDKInitializer.initialize(getActivity().getApplicationContext());
        initView();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_station:
                try {
                    net(etcity.getText().toString().trim(), etStation.getText().toString().trim(), getActivity());
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                    recycler_view.setLayoutManager(linearLayoutManager);
                    //设置适配器
                    myAdapter = new MyRecycleView(data);
                    recycler_view.setAdapter(myAdapter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    class MyRecycleView extends RecyclerView.Adapter<MyRecycleView.ViewHolder> {

        private List<BusStation.ResultBean> list;
        private Context mcontext;

        public MyRecycleView(List<BusStation.ResultBean> list) {
            this.list = list;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_layout, parent, false);
            MyRecycleView.ViewHolder viewHolder = new MyRecycleView.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            BusStation.ResultBean itemdata = list.get(position);
            holder.imageView.setBackgroundResource(R.drawable.app);
            holder.textView.setText(itemdata.getName());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            private TextView textView;
            private ImageView imageView;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.text_station_item);
                imageView = itemView.findViewById(R.id.image_station_item);
            }
        }
    }

    public static void net(final String city, final String station, final Activity context){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://op.juhe.cn/189/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Station service = retrofit.create(Station.class);//获取API接口的实现类的实例对象
        Call<BusStation> gsonnn = service.getStation("b5b3e2b45d5d4f8493e559aeaa07ef67",city, station );
        gsonnn.enqueue(new Callback<BusStation>() {
            @Override
            public void onResponse(Call<BusStation> call, Response<BusStation> response) {

                BusStation stat = response.body();
                List<BusStation.ResultBean> busStationList = (List<BusStation.ResultBean>) response.body();
                if (stat.getError_code() == 0) {
                    Toast.makeText(context, "获取成功"+busStationList.get(0).getName(), Toast.LENGTH_SHORT).show();
                }else if(stat.getError_code()==	213501){
                    Toast.makeText(context , "参数错误", Toast.LENGTH_SHORT).show();
                }else if (stat.getError_code()==213502||stat.getError_code()==213503) {
                    Toast.makeText(context, "城市与公交线路不匹配", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<BusStation> call, Throwable t) {

            }
        });

    }

}
